package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.ManageAreaBean;
import com.koron.css2.baseConfig.mapper.ManageAreaMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 修改管理区域
 * @author 李攀
 *
 */
public class ManageAreaUpdate implements ServerInterface {
	
	private static Logger logger = LogManager.getLogger(ManageAreaUpdate.class);
	
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		ManageAreaMapper mapper = factory.getMapper(ManageAreaMapper.class);
		ManageAreaBean bean = null;
		try {
			bean = JsonUtils.objectToPojo(req.getData(), ManageAreaBean.class);

			bean.setUpdateName(userInfo.getUserInfo().getName());
			bean.setUpdateTime(CommonUtils.getCurrentTime());
			if (StringUtils.isBlank(bean.getManageAreaId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
			}
			//查询原数据，比较是否修改父级目录
			ManageAreaBean oldBean = mapper.findManageAreaById(bean.getManageAreaId()+"");
			if(!(oldBean.getParentId()+"").equals((bean.getParentId()+""))) {
				//重新生成code
				ManageAreaAdd.handCode(mapper, bean);
			}
			mapper.updateManageArea(bean);
		} catch (Exception e) {
			logger.error("非法参数",e);
			factory.close(false);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}

}
